<?php
namespace Home\Controller;
use Common\Controller\CommonController;

/**
 * 
 * @author 张亚伟 <zhangyw@zhijin101.com>
 */
class PrizesController extends CommonController
{
    private $type_arr = ['1'=>'品牌页顶部图片'];
    private $winning_status = ['1'=>'等待用户填写收货信息', '2'=>'等待填写物流信息', '3'=>'已完成'];
    public function index()
    {
        $model = M('prizes');
        $Count = $model->count();
        $Page = new \Think\Pager($Count,10);
        $show = $Page->showhtml();
        $where = [];

        $list = $model->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        
        $this->assign('page',$show);
        $this -> assign('list', $list);
        $this -> display();
    }

    public function add()
    {
        if(IS_POST){
            $data = I('post.');
            if(empty($data['list_img'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传列表图片'));
            }
            if(empty($data['img'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传图片'));
            }

            $data['content'] = !empty($data['content']) ? html_entity_decode($data['content']) : '';

            $model = M('prizes');

            $id = $model->add($data);
            if($id){
                $this->ajaxReturn(array('s'=>true,'m'=>'添加成功', 'url'=>U('index')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'添加失败!'));
            }
        }else{
            $this->display();
        }
    }

    public function edit()
    {
        $id = I('id');
        if(IS_POST){
            $data = I('post.');
            if(empty($data['list_img'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传列表图片'));
            }
            if(empty($data['img'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传图片'));
            }

            $data['content'] = !empty($data['content']) ? html_entity_decode($data['content']) : '';

            $model = M('prizes');

            $res = $model->where(['id'=>$id])->save($data);
            if($res!==false){
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('index')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        }else{
            $info = M('prizes')->where(['id'=>$id])->find();
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function del(){
        $id = I('id');
        $res = M('prizes')->where(['id'=>$id])->delete();
        if($res){
            $this->ajaxReturn(array('s'=>true,'m'=>'删除成功', 'url'=>U('index')));
        }else{
            $this->ajaxReturn(array('s'=>false,'m'=>'删除失败!'));
        }
    }

    public function winning()
    {
        $model = M('winning');
        $Count = $model->count();
        $Page = new \Think\Pager($Count,10);
        $show = $Page->showhtml();
        $where = [];

        $list = $model->alias('w')
                ->join('__PRIZES__ p ON w.prizes_id=p.id', 'left')
                ->join('__USER__ u ON w.user_id=u.id', 'left')
                ->where($where)
                ->limit($Page->firstRow.','.$Page->listRows)
                ->field('w.*,u.nickname,p.title,p.list_img')
                ->order('w.id desc')->select();
        
        $this->assign('page',$show);
        $this->assign('winning_status',$this->winning_status);
        $this -> assign('list', $list);
        $this -> display();        
    }

    public function winning_edit()
    {
        $id = I('id');
        if(IS_POST){
            $data = I('post.');
            if(empty($data['user_name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入姓名'));
            }
            if(empty($data['mobile'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入电话'));
            }
            if(empty($data['address'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入地址'));
            }
            if(!empty($data['express']) && !empty($data['express_number'])){
                $data['status'] = 3;
            }
            $model = M('winning');

            $res = $model->where(['id'=>$id])->save($data);
            if($res!==false){
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('winning')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        }else{
            $info = M('winning')->alias('w')
                ->join('__PRIZES__ p ON w.prizes_id=p.id', 'left')
                ->join('__USER__ u ON w.user_id=u.id', 'left')
                ->where(['w.id'=>$id])
                ->field('w.*,u.nickname,p.title,p.list_img')
                ->find();
            $this->assign('info', $info);
            $this->assign('winning_status', $this->winning_status);
            $this->display();
        }        
    }

    public function winning_del()
    {
        $id = I('id');
        $res = M('winning')->where(['id'=>$id])->delete();
        if($res){
            $this->ajaxReturn(array('s'=>true,'m'=>'删除成功', 'url'=>U('winning')));
        }else{
            $this->ajaxReturn(array('s'=>false,'m'=>'删除失败!'));
        }
    }
}
